import { BasicLayout, BlankLayout } from '@/layouts'
import menuNav from '@/router/menuNav'

import store from '../store/index'
// 前端路由表
const constantRouterComponents = {
  // 基础页面 layout 必须引入
  BasicLayout: BasicLayout,
  BlankLayout: BlankLayout,

  // 你需要动态引入的页面组件
  // 考勤管理
  AttendanceConvertExcel: () => import('@/views/attendance/convertExcel'),
  AttendanceSpecialStaff: () => import('@/views/attendance/specialStaff'),
  StatisticalData: () => import('@/views/attendance/statisticalData'),
  DailyStatistics: () => import('@/views/attendance/dailyStatistics'),
  SpecialDailyStatistics: () => import('@/views/attendance/specialDailyStatistics'),
  AttendanceShiftSetting: () => import('@/views/attendance/shiftSetting'),
  AttendanceCrewRostering: () => import('@/views/attendance/crewRostering'),
  AttendanceShiftScheduling: () => import('@/views/attendance/shiftScheduling'),
  EntryAndExitRecords: () => import('@/views/attendance/entryAndExitRecords'),
  // 排班管理
  UsersList: () => import('@/views/plan/usersList'),
  PlanList: () => import('@/views/plan/planList'),
  BatchPlan: () => import('@/views/plan/batchPlan'),
  BatchSetting: () => import('@/views/plan/batchSetting'),
  SpecialPlanSetting: () => import('@/views/plan/specialPlanSetting'),
  // 人员管理
  PeopleList: () => import('@/views/people/peopleList'),
  WorkList: () => import('@/views/people/workList'),

  // 管理员管理
  ManagerList: () => import('@/views/manager/managerList'),
  MenuList: () => import('@/views/manager/menuList'),
 
}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*',
  redirect: '/404',
  hidden: true
} 

/**
 * 动态生成菜单
 * @param token
 * @returns {Promise<Router>}
 */
export const getRouter = () => {
  const routers = generator(menuNav)
  routers.push(notFoundRouter)
  return routers
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap, parent) => {
  return routerMap.map(item => {
    const { title, hidden, hideChildren, hiddenHeaderContent, target, icon } = item.meta || {}
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.path || `/${item.key}`,
      // 路由名称，建议唯一
      name: item.name || item.key || '',
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.key],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: (constantRouterComponents[item.component || item.key]) || (() => import(`@/views/${item.component}`)),

      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: title,
        pageTitle: parent ? `${parent.meta.title}/${title}` : '',
        icon: icon || undefined,
        hiddenHeaderContent: hiddenHeaderContent,
        target: target,
        // permission: item.name,
        permission: item.permission || item.name
      }
    }
    // 是否设置了隐藏菜单
    if (hidden === true) {
      currentRouter.hidden = hidden
    }
    // 是否设置了隐藏子菜单
    if (hideChildren === true) {
      currentRouter.hideChildrenInMenu = hideChildren
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith('http')) {
      currentRouter.path = currentRouter.path.replace('//', '/')
    }
    // 重定向
    item.redirect && (currentRouter.redirect = item.redirect)
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generator(item.children, currentRouter)
    }
    return currentRouter
  })
}
